package com.mo.demo.jsr107;

import lombok.extern.slf4j.Slf4j;

import javax.cache.*;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.expiry.CreatedExpiryPolicy;
import javax.cache.expiry.Duration;
import javax.cache.spi.CachingProvider;

@Slf4j
public class Jsr107Demo {
    public static void main(String[] args) {
        // 从应用程序的类路径中检索默认的 CachingProvider 实现。当且仅当类路径中只有一个 JCache 实现 jar 时，此方法才会起作用。如果您的类路径中有多个提供者，则使用完全限定名称org.ehcache.jsr107.EhcacheCachingProvider来检索 Ehcache 缓存提供者。您可以通过使用Caching.getCachingProvider(String)静态方法来做到这一点。
        CachingProvider cachingProvider = Caching.getCachingProvider();

        // CacheManager使用提供程序检索默认实例。
        CacheManager cacheManager = cachingProvider.getCacheManager();

        MutableConfiguration<Long, String> configuration
                = new MutableConfiguration<Long, String>()   // 使用MutableConfiguration...创建缓存配置
                .setTypes(Long.class, String.class) // 设置缓存key和value的类型
                .setStoreByValue(false) // 配置为按引用（而不是按值）存储缓存条目
                .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(Duration.ONE_MINUTE)); // 配置缓存条目创建1分钟后过期

        // 创建 myCache 缓存
        Cache<Long, String> myCache = cacheManager.createCache("myCache", configuration);

        myCache.put(1L,"1111");

        log.info("value={}",myCache.get(1L));


        // 输出
        // 05:40:54.622 [main] DEBUG org.ehcache.core.spi.ServiceLocator - Starting 18 Services...
        // 05:40:54.622 [main] DEBUG org.ehcache.impl.internal.statistics.DefaultStatisticsService - Starting service
        // 05:40:54.622 [main] DEBUG org.ehcache.core.spi.ServiceLocator - All Services successfully started, 18 Services in 15ms
        // 05:40:54.622 [main] DEBUG org.ehcache.impl.internal.statistics.DefaultStatisticsService - Moving from UNINITIALIZED to AVAILABLE
        // 05:40:54.622 [main] DEBUG org.ehcache.core.EhcacheManager - Initialize successful.
        // 05:40:54.779 [main] DEBUG org.ehcache.core.EhcacheManager - Creating Cache 'myCache' in EhcacheManager.
        // 05:40:54.779 [main] DEBUG org.ehcache.impl.internal.spi.serialization.DefaultSerializationProvider - Serializer for <java.lang.Long> : org.ehcache.impl.serialization.LongSerializer@4a7f959b
        // 05:40:54.779 [main] DEBUG org.ehcache.impl.internal.spi.serialization.DefaultSerializationProvider - Serializer for <java.lang.String> : org.ehcache.impl.serialization.StringSerializer@67c33749
        // 05:40:54.794 [main] DEBUG org.ehcache.impl.internal.spi.copy.DefaultCopyProvider - Copier for <java.lang.Long> : org.ehcache.impl.copy.IdentityCopier@52af26ee
        // 05:40:54.794 [main] DEBUG org.ehcache.impl.internal.spi.copy.DefaultCopyProvider - Copier for <java.lang.String> : org.ehcache.impl.copy.IdentityCopier@6fd83fc1
        // 05:40:54.810 [main] DEBUG org.ehcache.impl.internal.store.heap.OnHeapStore - TTI or custom expiration strategy detected
        // 05:40:54.872 [main] DEBUG class org.ehcache.core.Ehcache-myCache - Initialize successful.
        // 05:40:54.872 [main] DEBUG org.ehcache.impl.internal.statistics.DefaultStatisticsService - Cache added myCache
        // 05:40:54.888 [main] INFO org.ehcache.core.EhcacheManager - Cache 'myCache' created in EhcacheManager.
        // 05:40:54.919 [main] INFO com.mo.demo.jsr107.Jsr107Demo - value=1111
    }
}
